//
//  ProfileView.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import SwiftUI

struct ProfileView: View {
    @EnvironmentObject var profileViewModel: ProfileViewModel
    
    var body: some View {
        NavigationView {
            List {
                // 个人信息头部
                ProfileHeaderView()
                    .environmentObject(profileViewModel)
                    .listRowInsets(EdgeInsets())
                    .listRowSeparator(.hidden)
                
                // 功能菜单
                ForEach(Array(profileViewModel.menuItems.enumerated()), id: \.offset) { sectionIndex, section in
                    Section {
                        ForEach(section) { menuItem in
                            ProfileMenuItemView(menuItem: menuItem)
                                .environmentObject(profileViewModel)
                        }
                    }
                }
            }
            .listStyle(GroupedListStyle())
            .navigationTitle("我")
            .navigationBarTitleDisplayMode(.inline)
        }
        .sheet(isPresented: $profileViewModel.showEditProfile) {
            EditProfileView()
                .environmentObject(profileViewModel)
        }
        .sheet(isPresented: $profileViewModel.showSettings) {
            SettingsView()
        }
        .sheet(isPresented: $profileViewModel.showQRCode) {
            QRCodeView()
                .environmentObject(profileViewModel)
        }
        .sheet(isPresented: $profileViewModel.showPayment) {
            PaymentView()
        }
        .sheet(isPresented: $profileViewModel.showFavorites) {
            FavoritesView()
        }
        .sheet(isPresented: $profileViewModel.showPhotos) {
            PhotosView()
        }
        .sheet(isPresented: $profileViewModel.showCards) {
            CardsView()
        }
        .sheet(isPresented: $profileViewModel.showEmoji) {
            EmojiView()
        }
    }
}

struct ProfileHeaderView: View {
    @EnvironmentObject var profileViewModel: ProfileViewModel
    
    var body: some View {
        Button(action: {
            profileViewModel.showEditProfile = true
        }) {
            HStack(spacing: 15) {
                // 头像
                Image(systemName: profileViewModel.currentUser.avatar)
                    .font(.system(size: 50))
                    .foregroundColor(.blue)
                    .frame(width: 70, height: 70)
                    .background(Color.gray.opacity(0.2))
                    .clipShape(RoundedRectangle(cornerRadius: 12))
                
                VStack(alignment: .leading, spacing: 4) {
                    Text(profileViewModel.currentUser.displayName)
                        .font(.system(size: 20, weight: .medium))
                        .foregroundColor(.primary)
                    
                    Text("微信号: \(profileViewModel.currentUser.userName)")
                        .font(.system(size: 14))
                        .foregroundColor(.secondary)
                    
                    if !profileViewModel.currentUser.signature.isEmpty {
                        Text(profileViewModel.currentUser.signature)
                            .font(.system(size: 12))
                            .foregroundColor(.secondary)
                            .lineLimit(2)
                    }
                }
                
                Spacer()
                
                VStack(spacing: 8) {
                    Button(action: {
                        profileViewModel.showQRCode = true
                    }) {
                        Image(systemName: "qrcode")
                            .font(.system(size: 16))
                            .foregroundColor(.gray)
                    }
                    
                    Image(systemName: "chevron.right")
                        .font(.system(size: 14))
                        .foregroundColor(.secondary)
                }
            }
            .padding(.horizontal, 16)
            .padding(.vertical, 20)
        }
        .buttonStyle(PlainButtonStyle())
        .background(Color(UIColor.systemBackground))
    }
}

struct ProfileMenuItemView: View {
    let menuItem: ProfileViewModel.ProfileMenuItem
    @EnvironmentObject var profileViewModel: ProfileViewModel
    
    var body: some View {
        Button(action: {
            profileViewModel.handleMenuAction(menuItem.action)
        }) {
            HStack(spacing: 15) {
                Image(systemName: menuItem.icon)
                    .font(.system(size: 18))
                    .foregroundColor(getIconColor(for: menuItem.action))
                    .frame(width: 25)
                
                Text(menuItem.title)
                    .font(.system(size: 16))
                    .foregroundColor(.primary)
                
                Spacer()
                
                Image(systemName: "chevron.right")
                    .font(.system(size: 12))
                    .foregroundColor(.secondary)
            }
            .padding(.vertical, 2)
        }
        .buttonStyle(PlainButtonStyle())
    }
    
    private func getIconColor(for action: ProfileViewModel.ProfileMenuAction) -> Color {
        switch action {
        case .pay:
            return .green
        case .collect:
            return .orange
        case .photos:
            return .blue
        case .cards:
            return .purple
        case .emoji:
            return .yellow
        case .settings:
            return .gray
        }
    }
}

struct EditProfileView: View {
    @EnvironmentObject var profileViewModel: ProfileViewModel
    @Environment(\.presentationMode) var presentationMode
    @State private var displayName: String = ""
    @State private var signature: String = ""
    
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("个人信息")) {
                    HStack {
                        Text("头像")
                        Spacer()
                        Image(systemName: profileViewModel.currentUser.avatar)
                            .font(.system(size: 40))
                            .foregroundColor(.blue)
                            .frame(width: 50, height: 50)
                            .background(Color.gray.opacity(0.2))
                            .clipShape(RoundedRectangle(cornerRadius: 8))
                    }
                    
                    HStack {
                        Text("昵称")
                        TextField("请输入昵称", text: $displayName)
                            .multilineTextAlignment(.trailing)
                    }
                    
                    VStack(alignment: .leading) {
                        Text("个性签名")
                        TextEditor(text: $signature)
                            .frame(minHeight: 60)
                    }
                }
                
                Section(header: Text("账号信息")) {
                    HStack {
                        Text("微信号")
                        Spacer()
                        Text(profileViewModel.currentUser.userName)
                            .foregroundColor(.secondary)
                    }
                    
                    HStack {
                        Text("手机号")
                        Spacer()
                        Text(profileViewModel.currentUser.phoneNumber)
                            .foregroundColor(.secondary)
                    }
                }
            }
            .navigationTitle("个人信息")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("取消") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
                
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        profileViewModel.updateProfile(displayName: displayName, signature: signature)
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
        .onAppear {
            displayName = profileViewModel.currentUser.displayName
            signature = profileViewModel.currentUser.signature
        }
    }
}

struct SettingsView: View {
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        NavigationView {
            List {
                Section("账号与安全") {
                    SettingsRowView(icon: "lock.shield", title: "账号与安全", color: .green)
                    SettingsRowView(icon: "bell", title: "新消息通知", color: .orange)
                    SettingsRowView(icon: "eye.slash", title: "隐私", color: .blue)
                }
                
                Section("通用") {
                    SettingsRowView(icon: "textformat.size", title: "字体大小", color: .purple)
                    SettingsRowView(icon: "moon", title: "深色模式", color: .indigo)
                    SettingsRowView(icon: "globe", title: "多语言", color: .red)
                }
                
                Section("帮助与反馈") {
                    SettingsRowView(icon: "questionmark.circle", title: "帮助与反馈", color: .gray)
                    SettingsRowView(icon: "info.circle", title: "关于微信", color: .gray)
                }
                
                Section {
                    Button(action: {
                        // 退出登录
                    }) {
                        HStack {
                            Spacer()
                            Text("退出登录")
                                .foregroundColor(.red)
                            Spacer()
                        }
                    }
                }
            }
            .navigationTitle("设置")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
    }
}

struct SettingsRowView: View {
    let icon: String
    let title: String
    let color: Color
    
    var body: some View {
        HStack(spacing: 12) {
            Image(systemName: icon)
                .font(.system(size: 16))
                .foregroundColor(color)
                .frame(width: 20)
            
            Text(title)
                .font(.system(size: 16))
            
            Spacer()
            
            Image(systemName: "chevron.right")
                .font(.system(size: 12))
                .foregroundColor(.secondary)
        }
    }
}

struct QRCodeView: View {
    @EnvironmentObject var profileViewModel: ProfileViewModel
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        NavigationView {
            VStack(spacing: 30) {
                Spacer()
                
                VStack(spacing: 20) {
                    // 用户信息
                    HStack(spacing: 15) {
                        Image(systemName: profileViewModel.currentUser.avatar)
                            .font(.system(size: 40))
                            .foregroundColor(.blue)
                            .frame(width: 50, height: 50)
                            .background(Color.gray.opacity(0.2))
                            .clipShape(RoundedRectangle(cornerRadius: 8))
                        
                        VStack(alignment: .leading) {
                            Text(profileViewModel.currentUser.displayName)
                                .font(.system(size: 18, weight: .medium))
                            Text("微信号: \(profileViewModel.currentUser.userName)")
                                .font(.system(size: 14))
                                .foregroundColor(.secondary)
                        }
                        
                        Spacer()
                    }
                    
                    // 二维码
                    VStack(spacing: 15) {
                        Rectangle()
                            .fill(
                                LinearGradient(
                                    gradient: Gradient(colors: [.black, .gray]),
                                    startPoint: .topLeading,
                                    endPoint: .bottomTrailing
                                )
                            )
                            .frame(width: 200, height: 200)
                            .overlay(
                                Image(systemName: "qrcode")
                                    .font(.system(size: 100))
                                    .foregroundColor(.white)
                            )
                            .clipShape(RoundedRectangle(cornerRadius: 12))
                        
                        Text("扫一扫上面的二维码，加我微信")
                            .font(.system(size: 14))
                            .foregroundColor(.secondary)
                    }
                }
                .padding(.horizontal, 30)
                
                Spacer()
                
                // 底部按钮
                HStack(spacing: 30) {
                    Button(action: {
                        // 保存二维码
                    }) {
                        VStack(spacing: 5) {
                            Image(systemName: "square.and.arrow.down")
                                .font(.system(size: 24))
                            Text("保存到相册")
                                .font(.system(size: 12))
                        }
                        .foregroundColor(.green)
                    }
                    
                    Button(action: {
                        // 分享二维码
                    }) {
                        VStack(spacing: 5) {
                            Image(systemName: "square.and.arrow.up")
                                .font(.system(size: 24))
                            Text("发送给朋友")
                                .font(.system(size: 12))
                        }
                        .foregroundColor(.green)
                    }
                }
                .padding(.bottom, 50)
            }
            .navigationTitle("我的二维码")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
    }
}
